import java.awt.Checkbox;
import java.io.File;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Label;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.FillLayout;


public class TrainerGUI implements IGUI{

	protected Shell shlIafTrainer;
	private static final String soundFilePath = "Message 1.wav";
	private static AudioPlayer player = new AudioPlayer(new File(soundFilePath));
	private Label restTime;
	private Scale restScale;
	public Logic excs;
	private int i = 0;
	private boolean isStarted = false;
	private Label details;
	private Composite planComposite;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TrainerGUI window = new TrainerGUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	public TrainerGUI() {
		excs = new Logic(this);
	}



	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlIafTrainer.open();
		shlIafTrainer.layout();
		shlIafTrainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
		while (!shlIafTrainer.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	public void playSound(){
		player.run();
	}
	
	public void printExc(String msg){
		details.setText(msg);
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlIafTrainer = new Shell(SWT.DIALOG_TRIM);
		shlIafTrainer.setSize(591, 411);
		shlIafTrainer.setText("IAF - Trainer");
		{
			Composite contents = new Composite(shlIafTrainer, SWT.NONE);
			contents.setBackground(SWTResourceManager.getColor(176, 196, 222));
			contents.setBounds(0, 0, 585, 379);
			{
				final Button playPause = new Button(contents, SWT.NONE);
				playPause.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						if(!isStarted){
							excs.start();
							printExc(excs.getTitle() + "\n" + excs.getInfo());
							playPause.setText("המשך");
						}else{
							excs.shut();
							playPause.setText("מנוחה");
						}
					}
				});
				playPause.setBounds(371, 344, 68, 23);
				playPause.setText("\u05DE\u05E0\u05D5\u05D7\u05D4");
			}
			{
				details = new Label(contents, SWT.BORDER);
				details.setAlignment(SWT.CENTER);
				details.setBounds(406, 127, 167, 182);
			}
			{
				Label label = new Label(contents, SWT.CENTER);
				label.setFont(SWTResourceManager.getFont("Tahoma", 14, SWT.NORMAL));
				label.setBounds(379, 315, 49, 23);
				label.setText("48");
			}
			{
				restScale = new Scale(contents, SWT.NONE);
				restScale.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						int n = restScale.getSelection();
						restTime.setText(String.valueOf(n));
						excs.setTimer(n);
					}
				});
				restScale.setBounds(230, 62, 170, 42);
			}
			{
				Label restLabel = new Label(contents, SWT.NONE);
				restLabel.setAlignment(SWT.RIGHT);
				restLabel.setFont(SWTResourceManager.getFont("Tahoma", 14, SWT.NORMAL));
				restLabel.setBounds(479, 70, 94, 34);
				restLabel.setText("\u05D6\u05DE\u05DF \u05DE\u05E0\u05D5\u05D7\u05D4");
			}
			{
				restTime = new Label(contents, SWT.NONE);
				restTime.setFont(SWTResourceManager.getFont("Tahoma", 14, SWT.NORMAL));
				restTime.setBounds(406, 70, 49, 34);
				restTime.setText("0");
			}
			{
				Label planLabel = new Label(contents, SWT.NONE);
				planLabel.setAlignment(SWT.RIGHT);
				planLabel.setFont(SWTResourceManager.getFont("Tahoma", 14, SWT.NORMAL));
				planLabel.setBounds(99, 30, 118, 23);
				planLabel.setText(":\u05EA\u05D5\u05DB\u05E0\u05D9\u05EA \u05D0\u05D9\u05DE\u05D5\u05DF");
			}
			{
				Label seperator = new Label(contents, SWT.SEPARATOR | SWT.VERTICAL);
				seperator.setBounds(223, 30, 2, 339);
			}
			{
				Label excImage = new Label(contents, SWT.BORDER | SWT.WRAP);
				excImage.setAlignment(SWT.CENTER);
				excImage.setBounds(231, 127, 172, 182);
				Image img = SWTResourceManager.getImage("D:\\My Documents\\My Works\\Workspace\\Trainer GUI\\exc.jpg");
				double xScale = (double)(excImage.getBounds().height) / (double)(img.getImageData().height);
				ImageData imgData = img.getImageData().scaledTo((int) (img.getImageData().width*xScale), excImage.getBounds().height);
				img = new Image(Display.getDefault(), imgData);
				excImage.setImage(img);
				
			}
			{
				planComposite = new Composite(contents, SWT.NONE);
				planComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
				planComposite.setBounds(10, 62, 207, 305);
			}
		}
		
		//create training plan
		i=0;
		for(String title : this.excs.titles){
//			Composite comp = new Composite(planComposite, SWT.NONE);
			final Button button = new Button(planComposite, SWT.CHECK);
			button.setText(title);
//			Label label = new Label(comp, SWT.WRAP);
//			label.setText(title);
			button.setSelection(true);
			button.setVisible(true);
			button.setBounds(0, i*20, planComposite.getBounds().width, 20);
			button.addSelectionListener(new SelectionListener(){
				private int n = i;
				public void widgetDefaultSelected(SelectionEvent e) {
					// TODO Auto-generated method stub		
				}
				public void widgetSelected(SelectionEvent e) {
					excs.exerType[n] = button.getSelection();
				}
				
			});
			i++;
		}
		details.setText(excs.getTitle() + "\n" + excs.getInfo());
	}
}
